﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class Admin
    {
        private readonly KeRui.DAL.Admin dal = new KeRui.DAL.Admin();

        public int Add(KeRui.Model.Admin model, string Password)
        {
            return this.dal.Add(model, Password);
        }

        public List<KeRui.Model.Admin> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.Admin> list = new List<KeRui.Model.Admin>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.Admin item = new KeRui.Model.Admin();
                    if (dt.Rows[i]["UserID"].ToString() != "")
                    {
                        item.UserID = int.Parse(dt.Rows[i]["UserID"].ToString());
                    }
                    item.Username = dt.Rows[i]["Username"].ToString();
                    item.UserFlag = dt.Rows[i]["UserFlag"].ToString();
                    item.RoleIDs = dt.Rows[i]["RoleIDs"].ToString();
                    item.RoleNames = dt.Rows[i]["RoleNames"].ToString();
                    item.RoleFlags = dt.Rows[i]["RoleFlags"].ToString();
                    item.PlusFlag = dt.Rows[i]["PlusFlag"].ToString();
                    item.RealName = dt.Rows[i]["RealName"].ToString();
                    item.JobTitle = dt.Rows[i]["JobTitle"].ToString();
                    item.JobDept = dt.Rows[i]["JobDept"].ToString();
                    item.QQ = dt.Rows[i]["QQ"].ToString();
                    item.MSN = dt.Rows[i]["MSN"].ToString();
                    item.Email = dt.Rows[i]["Email"].ToString();
                    item.Mobile = dt.Rows[i]["Mobile"].ToString();
                    item.HomeTel = dt.Rows[i]["HomeTel"].ToString();
                    item.OfficeTel = dt.Rows[i]["OfficeTel"].ToString();
                    if (dt.Rows[i]["IsFounder"].ToString() != "")
                    {
                        item.IsFounder = int.Parse(dt.Rows[i]["IsFounder"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    item.Guid = dt.Rows[i]["Guid"].ToString();
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int UserID)
        {
            this.dal.Delete(UserID);
        }

        public DataSet GetCmsSiteChk()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsSite", "", " SiteID ", " SiteID,SiteName");
        }

        public List<KeRui.Model.Admin> GetList()
        {
            string strOrder = " UserName";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("Admin", pageSize, pageIndex, "", strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Admin> GetList(string strWhere)
        {
            string strOrder = " UserName";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("Admin", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.Admin> GetListByCache(string strCondition)
        {
            string cacheKey = "AdminList-" + strCondition;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList();
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.Admin>) cache;
        }

        public KeRui.Model.Admin GetModel(int UserID)
        {
            return this.dal.GetModel(UserID);
        }

        public KeRui.Model.Admin GetModelByCache(int UserID)
        {
            string cacheKey = "AdminModel-" + UserID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(UserID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.Admin) cache;
        }

        public static string GetRealName()
        {
            string realName = Utils.UrlDecode(Utils.GetCookie("manage_RealName"));
            if (realName == "")
            {
                KeRui.BLL.Admin admin = new KeRui.BLL.Admin();
                realName = admin.GetModel(KeRui.BLL.Account.GetLoginUserID()).RealName;
                Utils.WriteCookie("manage_RealName", Utils.UrlEncode(realName), 0x7e900, ConfigHelper.GetConfigString("CookieDomain"));
            }
            return realName;
        }

        public DataSet GetRoleChildFlag(int ParentID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("AdminFlag ", " ParentID =" + ParentID + " AND FlagType=0", " ID ", " ID,FlagName,HaveChildNav,Icon,Flag");
        }

        public DataSet GetRoleChk()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("AdminRole", "", " RoleID ", " RoleID,RoleName");
        }

        public static string GetUsername()
        {
            string username = Utils.UrlDecode(Utils.GetCookie("manage_Username"));
            if (username == "")
            {
                KeRui.BLL.Admin admin = new KeRui.BLL.Admin();
                username = admin.GetModel(KeRui.BLL.Account.GetLoginUserID()).Username;
                Utils.WriteCookie("manage_Username", Utils.UrlEncode(username), 0x7e900, ConfigHelper.GetConfigString("CookieDomain"));
            }
            return username;
        }

        public static void SetRealName(string RealName)
        {
            Utils.WriteCookie("manage_RealName", Utils.UrlEncode(RealName), 0x7e900, ConfigHelper.GetConfigString("CookieDomain"));
        }

        public static void SetUsername(string Username)
        {
            Utils.WriteCookie("manage_Username", Utils.UrlEncode(Username), 0x7e900, ConfigHelper.GetConfigString("CookieDomain"));
        }

        public int Update(KeRui.Model.Admin model, string Password)
        {
            return this.dal.Update(model, Password);
        }

        public int UpdateAdminInfo(KeRui.Model.Admin model)
        {
            return this.dal.updateAdminInfo(model);
        }

        public int UpdateForPlusSysRoleFlag(KeRui.Model.Admin model)
        {
            return this.dal.UpdateForPlusSysRoleFlag(model);
        }

        public int UpdatePlusFlagForSiteFlag(KeRui.Model.Admin model)
        {
            return this.dal.UpdateForPlusSysRoleFlag(model);
        }
    }
}

